/**
 * 添加产品类型
 */
layui.use(['layer', 'form', 'admin', 'ax', 'table', 'func', 'jquery'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;




    //初始化所有的产品类型列表
    $.ajax({
        url: Feng.ctxPath + '/productType/list',
        dataType: 'json',
        type: 'get',
        data: {},
        success: function (data) {
            $.each(data.data, function (index, value) {
                $('#productType').append(new Option(value.name,value.id));// 下拉菜单里添加元素
            });
            layui.form.render('select');
        }
    });

    var blankRowData = {
       'id':'', 'attrName': '', 'attrValue': ''
    };
    var tableData = [];
    table.render({
        elem: '#attrTable',
        id: "attrTable",
        cellMinWidth: 95,
        // toolbar: "#toolbar",
        defaultToolbar: [],
        height: "full-160",
        text: {none: "暂无数据"},
        page: false,
        cols: [
            [
                {
                    field: 'id',
                    title: 'ID',
                    hide: true
                },
                {
                    type: "checkbox",
                    fixed: 'left',
                    width: "10%",
                },
                {
                    field: 'attrName',
                    title: '属性',
                    width: "30%",
                    align: 'center',
                    edit: 'text'
                },
                {
                    field: 'attrValue',
                    title: '单位',
                    width: "40%",
                    align: 'center',
                    edit: 'text'
                },
                {
                    title: '操作',
                    width: "20%",
                    align: 'center',
                    toolbar: "#tableBar"
                }
            ]
        ],
        data: tableData
    });


    //添加表单数据
    $('#addAttrBtn').on('click',function(){
        var oldData = table.cache["attrTable"];
        var id = oldData.length + 1;
        blankRowData['id'] = id;
        oldData.push(blankRowData);
        table.reload('attrTable',{data: oldData});
    });

    //删除选中的数据
    $("#delAttrBtn").on('click', function (){
        var oldData = table.cache["attrTable"];
        //删除选中的数据
        var checkStatus = table.checkStatus('attrTable');
        var checkData = checkStatus.data;
        for(var i=0;i<oldData.length;i++){
            for(var j=0;j<checkData.length;j++){
                if(checkData[j].id == oldData[i].id){
                    oldData.splice(j, 1);
                }
            }
        }
        table.reload('attrTable',{data: oldData});
    });



    // /*增加/删除一行点击事件*/
    table.on("tool(attrTable)", function (e) {
        var oldData = table.cache["attrTable"];
        var data = e.data;
        if(e.event == 'delete'){
            for(var i=0;i<oldData.length;i++){
                if(oldData[i].id == data.id){
                    oldData.splice(i,1);
                }
            }
            table.reload('attrTable',{data: oldData});
        }
    });



    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var attrData = table.cache["attrTable"];
        var json = {};
        for(var i=0;i<attrData.length;i++){
            var attr = attrData[i];
            json[attr.attrName] = attr.attrValue;
        }
        data.field.attrContext = JSON.stringify(json);


        var ajax = new $ax(Feng.ctxPath + "/productAttr/add", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });
});